import 'package:environmental_les_app/common/routes/route_names.dart';
import 'package:environmental_les_app/common/style/colours.dart';
import 'package:environmental_les_app/common/widgets/add_picture.dart';
import 'package:environmental_les_app/common/widgets/attachment_select_widget.dart';
import 'package:environmental_les_app/common/widgets/bottom_select_drawer.dart';
import 'package:environmental_les_app/common/widgets/button_public_rect.dart';
import 'package:environmental_les_app/common/widgets/common_widget.dart';
import 'package:environmental_les_app/common/widgets/required_field_text.dart';
import 'package:environmental_les_app/common/widgets/text_area.dart';
import 'package:flutter_clx_base/flutter_clx_base.dart';

import 'index.dart';

class PatrolPage extends GetView<PatrolController> {
  const PatrolPage({super.key});

  // 主视图
  List<Widget> _buildView() {
    return [
      baseCardContainer(
          padding:
              const EdgeInsets.only(top: 0, left: 12, right: 12, bottom: 16),
          content: [
            operateRowItem(
                title: "巡查类型",
                content: "",
                isRequired: true,
                rightWidget: Row(
                  children: [
                    customRadio(
                      value: 1,
                      groupValue: controller.patrolType,
                      onChanged: (value) {
                        controller.patrolType = value!;
                        controller.update(["patrol"]);
                      },
                      title: '事件巡查',
                    ),
                    hGap20,
                    customRadio(
                      value: 2,
                      groupValue: controller.patrolType,
                      onChanged: (value) {
                        controller.patrolType = value!;
                        controller.update(["patrol"]);
                      },
                      title: '设备巡查',
                    ),
                  ],
                ),
                showLine: true),
            operateRowItem(
                title: "业务来源",
                hintText: "请选择业务来源",
                content: controller.selectedTaskSource['name'] ?? '',
                isRequired: true,
                onTap: () {
                  var contentList = controller.taskSourceList
                      .map<String>((e) => e['name'])
                      .toList();

                  Get.bottomSheet(BottomSelectDrawer(
                    title: "业务来源",
                    contentList: contentList,
                    completeSelect: (data, index) {
                      controller.modelSelect(index);
                    },
                  ));
                },
                showArrow: true,
                showLine: true),
            operateRowItem(
                title: "业务类型",
                hintText: "请选择业务类型",
                content: controller.selectedBusinessType['name'] ?? '',
                isRequired: true,
                onTap: () {
                  var contentList = controller.businessTypeList
                      .map<String>((e) => e['name'])
                      .toList();

                  Get.bottomSheet(BottomSelectDrawer(
                    title: "业务类型",
                    contentList: contentList,
                    completeSelect: (data, index) {
                      controller.selectedBusinessType =
                          controller.businessTypeList[index];
                      controller.update(["patrol"]);
                    },
                  ));
                },
                showArrow: true,
                showLine: true),
            operateRowItem(
                title: "事件地点",
                hintText: "请选择事件地点",
                content: "大气站",
                isRequired: true,
                onTap: () {
                  Get.toNamed(AppRoutes.locationSelectPage);
                },
                showArrow: true,
                showLine: true),
            operateRowItem(
                title: "详细地址",
                content: "北京市海淀区上地十街10号",
                isRequired: true,
                showArrow: false,
                showLine: true),
            operateRowItem(
                title: "巡查结果",
                content: "",
                isRequired: true,
                rightWidget: Row(
                  children: [
                    customRadio(
                      value: 1,
                      groupValue: controller.patrolResult,
                      onChanged: (value) {
                        controller.patrolResult = value!;
                        controller.update(["patrol"]);
                      },
                      title: '正常',
                    ),
                    hGap20,
                    customRadio(
                      value: 2,
                      groupValue: controller.patrolResult,
                      onChanged: (value) {
                        controller.patrolResult = value!;
                        controller.update(["patrol"]);
                      },
                      title: '异常',
                    ),
                  ],
                ),
                showLine: true),
            operateRowItem(
                title: "巡查开始时间",
                content: "2023-03-15 10:00",
                isRequired: true,
                onTap: () {},
                showArrow: true,
                showLine: true),
            operateRowItem(
                title: "巡查完成时间",
                content: "2023-03-15 10:00",
                isRequired: true,
                onTap: () {},
                showArrow: true,
                showLine: true),
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                vGap10,
                const RequiredFieldText(
                  text: "巡查描述",
                  titleColor: Colours.text969,
                  titleFontSize: 15,
                  titleFontWeight: FontWeight.w400,
                ),
                vGap16,
                TextArea(
                  controller: TextEditingController(),
                  hintText: "请输入内容",
                  minLines: 2,
                  maxLines: 5,
                  maxLength: 100,
                ),
              ],
            ),
            vGap16,
            AttachmentSelectWidget(
              initialFiles: controller.attachments,
              onChanged: (files) {
                controller.attachments = files;
                controller.update(["patrol"]);
              },
            ),
            const Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                vGap16,
                Text("影像信息",
                    style: TextStyle(fontSize: 15, color: Colours.text969)),
                vGap10,
                AddPictureWidget(num: 6, imageList: []),
              ],
            )
          ])
    ];
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<PatrolController>(
      init: PatrolController(),
      id: "patrol",
      builder: (_) {
        return MyScaffold(
          title: "巡查登记",
          bottomButton: Container(
            padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 10),
            child: Row(
              children: [
                Expanded(
                    child: ButtonPublicRect(
                        text: "交办处理",
                        onPressed: () {
                          Get.toNamed(AppRoutes.reportProblemPage);
                        })),
                hGap10,
                Expanded(
                    child: ButtonPublicRect(
                        text: "直接处理",
                        onPressed: () {
                          Get.toNamed(AppRoutes.patrolFillingPage);
                        })),
              ],
            ),
          ),
          children: [
            ..._buildView(),
            vGap20,
          ],
        );
      },
    );
  }
}
